package com.cq.hd.member.controller;

import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.ResultGenerator;
import com.cq.hd.member.api.TbBusinessAgentApi;
import com.cq.hd.member.api.dto.*;
import com.cq.hd.member.api.vo.*;

import com.cq.hd.member.service.TbBusinessAgentService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商家经纪人关联表 前端控制器
 * </p>
 *
 * @author mYunYu
 * @since 2023-11-21
 */
@Slf4j
@RestController
public class TbBusinessAgentController implements TbBusinessAgentApi {

    @Autowired
    private TbBusinessAgentService businessAgentService;

    @ApiOperation(value = "经纪人列表")
    @ApiResponse(response = AgentPageVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<Page<AgentPageVo>> pageAgent(AgentPageDto agentPageDto) {
        return ResultGenerator.success(businessAgentService.pageAgent(agentPageDto));
    }

    @ApiOperation(value = "根据商家id批量获取商家经纪人关联数据")
    @ApiResponse(response = BusinessAgentVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<List<BusinessAgentVo>> listByBusinessIds(List<Long> businessIds) {
        return ResultGenerator.success(businessAgentService.listByBusinessIds(businessIds));
    }

    @ApiOperation(value = "批量获取商家跟指定经纪人的佣金比例")
    @Override
    public Result<Map<Long, BigDecimal>> getAgentCommissionRateByBusinessIds(Long userId, List<Long> businessIds) {
        return ResultGenerator.success(businessAgentService.getAgentCommissionRateByBusinessIds(userId, businessIds));
    }

    @Override
    public Result<BigDecimal> getSubAgentCommissionRate(Long userId, Long agentId) {
        return ResultGenerator.success(businessAgentService.getSubAgentCommissionRate(userId, agentId));
    }

    @Override
    public Result<BigDecimal> getAgentCommissionRate(Long userId, Long businessId) {
        return ResultGenerator.success(businessAgentService.getAgentCommissionRate(userId, businessId));
    }

    @ApiOperation(value = "商家解除绑定经纪人分销关系")
    @ApiImplicitParam(name = "businessAgentUnBindDto", value = "实体", required = true, paramType = "body", dataType = "BusinessAgentUnBindDto")
    @Override
    public Result<Boolean> unBind(@RequestBody BusinessAgentUnBindDto businessAgentUnBindDto) {
        return ResultGenerator.success(businessAgentService.unBind(businessAgentUnBindDto));
    }

    @ApiOperation(value = "角色管理主页")
    @ApiResponse(response = BusinessAgentIndexVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<BusinessAgentIndexVo> businessAgentIndex(BusinessAgentIndexDto businessAgentIndexDto) {
        return ResultGenerator.success(businessAgentService.businessAgentIndex(businessAgentIndexDto));
    }

    @ApiOperation(value = "我的经纪人分页条件列表")
    @ApiResponse(response = MyAgentPageVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<Page<MyAgentPageVo>> pageMyAgent(MyAgentPageDto myAgentPageDto) {
        Page<MyAgentPageVo> myAgentPageVoPage = businessAgentService.pageMyAgent(myAgentPageDto);
        return ResultGenerator.success(myAgentPageVoPage);
    }

    @ApiOperation(value = "我的策划人分页条件列表")
    @ApiResponse(response = MyBusinessPageVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<Page<MyBusinessPageVo>> pageMyBusiness(MyBusinessPageDto myBusinessPageDto) {
        Page<MyBusinessPageVo> myBusinessPageVoPage = businessAgentService.pageMyBusiness(myBusinessPageDto);
        return ResultGenerator.success(myBusinessPageVoPage);
    }

    @ApiOperation(value = "经纪人专属佣金设置")
    @ApiImplicitParam(name = "businessAgentCommissionSaveDtoList", value = "实体", required = true, paramType = "body", dataType = "BusinessAgentCommissionSaveDto")
    @Override
    public Result<Boolean> saveAgentCommission(@RequestBody BusinessAgentCommissionSaveDto businessAgentCommissionSaveDto) {
        return ResultGenerator.success(businessAgentService.saveAgentCommission(businessAgentCommissionSaveDto));
    }

    @ApiOperation(value = "已设置佣金和未设置佣金经纪人列表")
    @ApiResponse(response = BusinessAgentCommissionPageVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<Page<BusinessAgentCommissionPageVo>> pageAgentCommission(BusinessAgentCommissionPageDto businessAgentCommissionPageDto) {
        return ResultGenerator.success(businessAgentService.pageAgentCommission(businessAgentCommissionPageDto));
    }

    @ApiOperation(value = "分组成员列表")
    @ApiResponse(response = BusinessGroupAgentPageVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<Page<BusinessGroupAgentPageVo>> pageBusinessGroupAgent(BusinessGroupAgentPageDto businessGroupAgentPageDto) {
        return ResultGenerator.success(businessAgentService.pageBusinessGroupAgent(businessGroupAgentPageDto));
    }

    @ApiOperation(value = "删除分组成员")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    @ApiResponse(response = Boolean.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<Boolean> deleteBusinessGroupAgent(Long id, Long businessId, Long agentId) {
        return ResultGenerator.success(businessAgentService.deleteBusinessGroupAgent(id, businessId, agentId));
    }

    @ApiOperation(value = "根据商家id和经纪人id查找对应的关联数据")
    @ApiResponse(response = BusinessAgentVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<BusinessAgentVo> getBusinessAgentById(Long businessId, Long userId) {
        return ResultGenerator.success(businessAgentService.getBusinessAgentById(businessId, userId));
    }

    @ApiOperation(value = "选择团长列表（供创建活动时设置分销佣金调用）")
    @ApiResponse(response = BusinessSelectAgentGroupVo.class, code = 200, message = "接口返回对象参数")
    @Override
    public Result<List<BusinessSelectAgentGroupVo>> listBusinessSelectAgent(Long userId, Long businessId) {
        return ResultGenerator.success(businessAgentService.listBusinessSelectAgent(userId, businessId));
    }

    @ApiOperation(value = "根据条件查询经纪人数据")
    @Override
    public Result<List<BusinessAgentVo>> listByDto(BusinessAgentDto businessAgentDto) {
        return ResultGenerator.success(businessAgentService.listByDto(businessAgentDto));
    }

    @ApiOperation(value = "根据日期查询新入驻的经纪人数")
    @Override
    public Result<Integer> getNewCountByDate(String startTime, String endTime) {
        return ResultGenerator.success(businessAgentService.getNewCountByDate(startTime, endTime));
    }

    @ApiOperation(value = "更新商家与经纪人相关的统计字段")
    @Override
    public Result<Boolean> updateBusinessAgentOrder(@RequestBody UpdateBusinessAgentOrderDto updateBusinessAgentOrderDto) {
        return ResultGenerator.success(businessAgentService.updateBusinessAgentOrder(updateBusinessAgentOrderDto));
    }

    @ApiOperation(value = "更新一级经纪人与二级经纪人相关的统计字段")
    @Override
    public Result<Boolean> updateSubAgentOrder(@RequestBody UpdateSubAgentOrderDto updateSubAgentOrderDto) {
        return ResultGenerator.success(businessAgentService.updateSubAgentOrder(updateSubAgentOrderDto));
    }

    @ApiOperation(value = "批量删除分组成员，批量移动分组成员")
    @Override
    public Result<Boolean> batchChangeGroup(BusinessGroupBatchChangeGroupDto businessGroupBatchChangeGroupDto) {
        return ResultGenerator.success(businessAgentService.batchChangeGroup(businessGroupBatchChangeGroupDto));
    }

    @ApiOperation(value = "我的上级经纪人")
    @Override
    public Result<MyAgentVo> myParentAgent(Long userId) {
        return ResultGenerator.success(businessAgentService.myParentAgent(userId));
    }

    @ApiOperation(value = "我的策划人id集合")
    @Override
    public Result<List<Long>> myBusinessIds(Long userId) {
        return ResultGenerator.success(businessAgentService.myBusinessIds(userId));
    }
}
